package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.UtilPath;
import com.magicode.cms.dao.IMIndexDao;
import com.magicode.cms.entiy.base.BroadPlatform;
import com.magicode.cms.entiy.base.MIndex;
import com.magicode.cms.service.IMIndexService;

@Service("servicemindex")
public class MIndexServiceImpl extends BaseServiceImpl<MIndex,IMIndexDao> implements IMIndexService,UtilPath{

	@Autowired
	@Qualifier("daoMIndex")
	private IMIndexDao mIndexDao;

	public IMIndexDao getImIndexDao() {
		return mIndexDao;
	}

	public void setImIndexDao(IMIndexDao mIndexDao) {
		this.mIndexDao = mIndexDao;
	}

	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "programName");
		map.put(json_title, "节目名称");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "channelName");
		map.put(json_title, "频道");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "rating");
		map.put(json_title, "M指数");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "statisticsDate");
		map.put(json_title, "年月日");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "source");
		map.put(json_title, "来源");
		map.put("width", 100);
		beaninfo.add(map);
		return beaninfo;
	}

	@Override
	public String getHql(Map<String, String> querywhere) throws Exception {
		String hql ="from " +MIndex.class.getName()+" "+entity_alias_name+" ";
		hql +=" left join t.program as p ";
		hql += " left join t.source as s ";
		hql += " left join t.author as a ";
		hql +=" left join t.tpWeek as tp ";
		hql += " where 1 = 1 ";
	if(null!=querywhere && !querywhere.isEmpty()){
	/*	if(map.containsKey(field_article_id)){
			hql = hql+" ";
		}*/
		if(querywhere.containsKey(field_id)){
			hql = hql+" and "+entity_alias_name+".id='"+querywhere.get(field_id)+"'";
		}
		if(querywhere.containsKey("date")){
			hql = hql+" and "+entity_alias_name+".statisticsDate='"+querywhere.get("date")+"'";
		}
	}
	return hql;
	}

	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere,0, field_grid_size);
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put("programName", ""+entity_alias_name+".programName as programName ");
			gridsel.put("channelName", ""+entity_alias_name+".channelName as channelName ");
			gridsel.put("rating", ""+entity_alias_name+".rating as rating ");
			gridsel.put("statisticsDate", ""+entity_alias_name+".statisticsDate as statisticsDate ");
			gridsel.put("source", "s.title as source ");
			return this.queryMapDataList(hql, gridsel,start ,size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}

	@Override
	public IMIndexDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return mIndexDao;
	}

	@Override
	public List<BroadPlatform> getbroPlfList(String id) {
		// TODO Auto-generated method stub
		return null;
	}

}
